package com.karson.common.util;

import com.karson.common.enums.FileCategory;
import com.karson.common.request.FileRequest;

public class FileCategoryUtil {

    public static FileCategory detectFileCategory(FileRequest file) {
        String contentType = file.getFileType(); // eg. "application/octet-stream"
        String fileName = file.getFileName().toLowerCase();

        // 先根据 Content-Type 判断
        if (contentType != null) {
            if (contentType.startsWith("audio/")) {
                return FileCategory.AUDIO;
            } else if (contentType.startsWith("image/")) {
                return FileCategory.IMAGE;
            } else if (contentType.equals("text/vtt")) {
                return FileCategory.SUBTITLE;
            }
        }

        // 再根据文件后缀判断（兜底）
        if (fileName.endsWith(".srt") || fileName.endsWith(".vtt") || fileName.endsWith(".ass")) {
            return FileCategory.SUBTITLE;
        }

        return FileCategory.OTHER;
    }
}
